import axios from 'axios'
import type { AxiosInstance, AxiosRequestConfig } from 'axios'
import { RequestConfig } from './types'
// import type { RequestConfig, RequestInterceptors } from './types'

class service {
  instance: AxiosInstance
    static request: any

  constructor(config: AxiosRequestConfig) {
    //   实例
    this.instance = axios.create(config)
    // 全局请求拦截
    this.instance.interceptors.request.use(
      (res: AxiosRequestConfig) => {
        return res
      },
      (err: any) => err
    )
    // 全局响应拦截器
    this.instance.interceptors.response.use(
      (res: AxiosRequestConfig) => {
        return res
      },
      (err: any) => err
    )
  }

  //   request(config: AxiosRequestConfig) {
  //     return this.instance.request(config)
  //   }

  request<T>(config: RequestConfig): Promise<T> {
    return new Promise((resolve, reject) => {
      this.instance
        .request<any, T>(config)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          reject(err)
        })
    })
  }
}

export default service
